package com.thinksns.sociax.t4.android.widget;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;

/**
 * 可控制暂停, 继续, 获取计时时长的计时器
 * <p>
 * 重写start(), 从上次暂停处继续
 */
public class ControlChronometer extends Chronometer {

    private long mRecordTime;

    public ControlChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void start() {
        if (mRecordTime == 0) {
            setBase(SystemClock.elapsedRealtime());
        } else {
            setBase(getBase() + SystemClock.elapsedRealtime() - mRecordTime);
        }
        super.start();
    }


    @Override
    public void stop() {
        mRecordTime = SystemClock.elapsedRealtime();
        super.stop();
    }

    public void reset() {
        super.stop();
        setBase(SystemClock.elapsedRealtime());
        mRecordTime = 0;
    }

    public void resetBase(long jumpTimeMs) {
        setBase(SystemClock.elapsedRealtime() - jumpTimeMs);
        invalidate();
        super.start();
    }

    /**
     * 获取计时器总时长
     * unit : ms
     *
     * @return
     */
    public long getCountTime() {
        return mRecordTime - getBase();
    }
}